package com.jwgf.fxprotocolstack;

import java.util.Locale;
import java.util.Random;

import com.jwgf.Half485.fxthreadpool.FxUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class FxHandShakeTest {
    private static boolean isWriteSuccess = true;

    public FxHandShakeTest() {
        EventBus.getDefault().register(this);
    }


    @Subscribe
    public void OnFxCommandReceive(byte[] data) {
        try {
            int id = FxUtils.getRequestId(data);
            int addr = FxUtils.getRequestAddr(data);
            int len = FxUtils.getRequestComponentAmount(data);
            String command = FxUtils.getRequestCommand(data);
            if (command.equals("WR")) {
                switch (id) {
                    case 2:
                        byte[] data2Plc = generateReadReturnData(2, 0x0f);
//                        SmaroWindingMachineActivity.half485Stack.process(data2Plc);
                        break;
                    case 4:
                        byte[] data4Plc = generateReadReturnData(4, 0x17);
//                        SmaroWindingMachineActivity.half485Stack.process(data4Plc);
                        break;
                    case 5:
                        byte[] data5Plc = generateReadReturnData(5, 0x11);
//                        SmaroWindingMachineActivity.half485Stack.process(data5Plc);
                        break;
                    case 6:
//					byte[] data6Plc = generateReadReturnData(6, len);
//					SmaroWindingMachineActivity.half485Stack.process(data6Plc);
                        break;
                    case 8:
                        byte[] data8Plc = generateReadReturnData(8, 0x04);
//                        SmaroWindingMachineActivity.half485Stack.process(data8Plc);
                        break;
                    default:
                        break;

                }
            } else if (command.equals("WW")) {
                if (isWriteSuccess) {
                    byte[] writeReturnData = generateWriteSuccessReturnData(id);
//                    SmaroWindingMachineActivity.half485Stack.process(writeReturnData);
                } else {
                    byte[] writeReturnData = generateWriteFailedReturnData(id);
//                    SmaroWindingMachineActivity.half485Stack.process(writeReturnData);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public byte[] generateReadReturnData(int id, int len) {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("%2d", id));
        sb.append(FxUtils.PC);
        if (id == 2) {
            for (int i = 0; i < len; i++) {
                Random random = new Random();
                short rdata = (short) random.nextInt(65535);
                rdata = (short) (((rdata % 2) == 0) ? 0 : 1);
                sb.append(String.format("%04x", rdata).toUpperCase(Locale.getDefault()));

            }
        } else {
            for (int i = 0; i < len; i++) {
                Random random = new Random();
                short rdata = (short) random.nextInt(65535);
                sb.append(String.format("%04x", rdata).toUpperCase(Locale.getDefault()));

            }
        }

        sb.append(FxUtils.ETX);
        sb.append(FxUtils.sum(sb.toString()));
        sb.insert(0, FxUtils.STX);
        return sb.toString().getBytes();
    }

    public byte[] generateWriteSuccessReturnData(int id) {
        StringBuilder sb = new StringBuilder();
        sb.append((char) 0);
        sb.append(FxUtils.ACK);
        sb.append(String.format("%02d", id));
        sb.append(FxUtils.PC);
        return sb.toString().getBytes();
    }

    public byte[] generateWriteFailedReturnData(int id) {
        return new byte[]{0x02};
    }
}
